'use server'

import { prisma } from "@/lib/prisma"
import { Metadata } from "next"
import { Employee } from '@/types'

export const metadata: Metadata = {
  title: "员工业绩统计 - 商场销售系统",
  description: "员工业绩统计页面",
}

export default async function PerformanceReportPage() {
  // 获取所有员工的业绩数据
  const employees = await prisma.employee.findMany({
    include: {
      sales: true
    },
    orderBy: {
      createdAt: 'desc',
    },
  })

  // 计算每个员工的总销售额
  const performanceData = employees.map(employee => {
    const totalSales = employee.sales.reduce((sum, sale) => {
      return sum + Number(sale.amount)
    }, 0)

    return {
      id: employee.id,
      name: employee.name,
      totalSales,
    }
  })

  return (
    <div className="space-y-4 p-8">
      <h1 className="text-2xl font-bold">员工业绩统计</h1>
      <table className="min-w-full mt-4 border-collapse border border-gray-300">
        <thead>
          <tr>
            <th className="border border-gray-300 p-2">员工姓名</th>
            <th className="border border-gray-300 p-2">总销售额</th>
          </tr>
        </thead>
        <tbody>
          {performanceData.map(employee => (
            <tr key={employee.id}>
              <td className="border border-gray-300 p-2">{employee.name}</td>
              <td className="border border-gray-300 p-2">¥{employee.totalSales.toFixed(2)}</td>
            </tr>
          ))}
        </tbody>
      </table>
    </div>
  )
} 